package cn.learn.validator;

import cn.learn.validator.annotation.ValidList;
import cn.learn.validator.groupby.Employee;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.groups.Default;
import java.util.List;

/**
 * 打开swagger测试
 * 两个标签的区别：@Valid、@Validated
 * {@link Valid}：没有分组的功能。
 * {@link Valid}：可以用在方法、构造函数、方法参数和成员属性（字段）上
 * {@link Validated}：提供了一个分组功能，可以在入参验证时，根据不同的分组采用不同的验证机制
 * {@link Validated}：可以用在类型、方法和方法参数上。但是不能用在成员属性
 *
 * @author huangyezhan
 * @version 1.0
 * @date 2020年08月01日15:44
 */
@Validated
@RestController
@RequestMapping("/valid")
public class ValidatorController {


    /**
     * 检验标签
     * 用MockMvc测试不会打印出异常原因Valid
     */
    @RequestMapping(value = "user", method = RequestMethod.POST)
    public User valid(@Valid @RequestBody User user) {
        System.out.println("=============================");
        System.out.println(user);
        user.setAccountName("已经访问结束了");
        return user;
    }

    /**
     * 检验标签
     */
    @RequestMapping(value = "user2", method = RequestMethod.POST)
    public User valid2(@RequestBody User user) {
        user.setAccountName("已经");
        System.out.println("=============================");
        System.out.println(user);
        return user;
    }

    /**
     * 检验标签
     */
    @RequestMapping(value = "user3", method = RequestMethod.POST)
    public User valid3(User user) {
        System.out.println("=============================");
        System.out.println(user);
        user.setAccountName("end");
        return user;
    }

    /**
     * 检验标签
     */
    @RequestMapping(value = "user4", method = RequestMethod.POST)
    public User valid4(@RequestBody User user) {
        System.out.println("=============================");
        System.out.println(user);
        user.setAccountName("end");
        return user;
    }


    /**
     * 检验标签
     */
    @RequestMapping(value = "user5", method = RequestMethod.GET)
    public User valid5(@RequestParam("password") @Valid @Length(min = 6, max = 8) String password) {
        User user = new User();
        user.setAccountName("end");
        System.out.println("密码：" + password);
        return user;
    }

    /**
     * 分组的数据校验
     *
     * @param employee 员工
     * @return 返回员工信息
     */
    @RequestMapping(value = "employee", method = RequestMethod.POST)
    public Employee valid(@RequestBody @Valid Employee employee) {
        employee.setUsername("已经来过了！！！");
        return employee;
    }


    /**
     * 分组的数据校验
     *
     * @param employee 员工
     * @return 返回员工信息
     */
    @RequestMapping(value = "groupBy", method = RequestMethod.POST)
    public Employee valid6(@RequestBody @Validated({Employee.Add.class, Default.class}) Employee employee) {

        System.out.println("=============");
        return employee;
    }

    /**
     * list集合——分组的数据校验
     *
     * @param employees 员工
     * @return 返回员工信息
     */
    @RequestMapping(value = "groupByList", method = RequestMethod.POST)
    public List<Employee> valid7(@RequestBody @ValidList(listGroups = {Employee.Add.class, Default.class}) List<Employee> employees) {

        System.out.println("=============");
        return employees;
    }

}
